package com.javasm.controller.JINXIAOController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.commons.basic.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.entity.JINXIAOEntity.SysDept;
import com.javasm.dao.JINXIAODao.SysDeptDao;
import com.javasm.service.JINXIAOService.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("sysDept")
//跨域问题  方式1 ：@CrossOrigin 注解  方式2：application.yml配置
//@CrossOrigin(origins = "http://localhost:8088",methods = {RequestMethod.GET,RequestMethod.POST},allowedHeaders = "*")
public class SysDeptController extends BaseController {
    @Resource
    private SysDeptService sysDeptService;

    @Autowired
    private SysDeptDao sysDeptDao;



    @GetMapping("/getDeptsTree")
    public void getDeptsTree() throws IOException {
        List<SysDept> sysDepts = sysDeptDao.queryAll(null);
        //递归转树形结构数据
        List<SysDept> treeList = sysDeptService.buildTree(sysDepts, "-1");
    }
   //根据部门名称查询部门树型结构
    @GetMapping("/tree")
    public AxiosResult getDeptTree(String deptName){
        List<SysDept> list = sysDeptService.deptTree(deptName);
        return suc(list);
    }


    @GetMapping("{id}")
    public AxiosResult selectById(@PathVariable String id)
    {
        SysDept obj = this.sysDeptService.queryById(id);
        return suc(obj);
    }
    @SuppressWarnings("unchecked")
    @GetMapping("ids/{ids}")
    public AxiosResult selectListByIds(@PathVariable String ids)
    {
        String[] idsArray = ids.split(",");
        List<SysDept> list = this.sysDeptService.queryByIds(Arrays.asList(idsArray));
        return suc(list);
    }
    @SuppressWarnings("unchecked")//List没有使用泛型， .@SuppressWarnings("unchecked") 隐藏所有的不安全的警告信息
    @GetMapping("list")
    public AxiosResult selectList(SysDept obj, Integer pageNum, Integer pageSize)
    {
        if(pageNum==null) pageNum=1;
        if(pageSize==null) pageSize=10;
        PageHelper.startPage(pageNum,pageSize);
        List<SysDept> list = this.sysDeptService.queryList(obj);
        PageInfo i = new PageInfo(list);
        return suc(i);
    }

    @PostMapping
    public AxiosResult add(@RequestBody SysDept obj)
    {
        Boolean r = this.sysDeptService.insert(obj);
        return isok(r);
    }

    @PutMapping
    public AxiosResult update(@RequestBody SysDept obj)
    {
        Boolean r = this.sysDeptService.update(obj);
        return isok(r);
    }

    @DeleteMapping("{ids}")
    public AxiosResult delByIds(@PathVariable String ids)
    {
        String[] idsArray = ids.split(",");
        Boolean r = this.sysDeptService.deleteByIds(Arrays.asList(idsArray));
        return isok(r);
    }

}